/*******************************************************************************
 * File name: 		board.c
 * Description: 
 * Project: 		_htx
 * Target: 			LPC2478
 * Compiler: 		arm-none-eabi-gcc
 * Date: 			31-01-2012
 * Author: 			Kuba, PawelKobylecki
 * Based on: 		---
 ******************************************************************************/
// Definicje diod led i switch'y dla p�yty ewaluacyjnej Olimex, oraz pakietu
// procesor�w NXP3 ver.1.0.

/*==============================================================================
 Includes
==============================================================================*/
# include "LPC24xx.h"

# include "board.h"
/*==============================================================================
 Defines
==============================================================================*/
# if OLIMEX
# define LED_DIR     FIO1DIR
# define LED0_PIN    FIO1PIN
# define LED1_PIN    FIO1PIN
# define LED0_ON     FIO1CLR
# define LED1_ON     FIO1CLR
# define LED0_OFF    FIO1SET
# define LED1_OFF    FIO1SET
# define LED0        (1 << 18)
# define LED1        (1 << 13)
# endif

# if CANDC
# if PG /* PG (procesor graficzny) */
#define LED_P0_12     (1 << 12)
#define LED_P0_13     (1 << 13)
#define LED_P0_28     (1 << 28)
#define LED_P0_27     (1 << 27)
#define LED_P0_31     (1 << 31)

#define LED_P0_12_ON    FIO0CLR
#define LED_P0_13_ON    FIO0CLR
#define LED_P0_28_ON    FIO0CLR
#define LED_P0_27_ON    FIO0CLR
#define LED_P0_31_ON    FIO0CLR

#define LED_P0_12_OFF   FIO0SET
#define LED_P0_13_OFF   FIO0SET
#define LED_P0_28_OFF   FIO0SET
#define LED_P0_27_OFF   FIO0SET
#define LED_P0_31_OFF   FIO0SET

#define LED_P0_12_PIN   FIO0PIN
#define LED_P0_13_PIN   FIO0PIN
#define LED_P0_28_PIN   FIO0PIN
#define LED_P0_27_PIN   FIO0PIN
#define LED_P0_31_PIN   FIO0PIN

#define LED_P0_12_DIR   FIO0DIR
#define LED_P0_13_DIR   FIO0DIR
#define LED_P0_28_DIR   FIO0DIR
#define LED_P0_27_DIR   FIO0DIR
#define LED_P0_27_DIR   FIO0DIR

#define K1_P2_31      (1 << 31)
#define K2_P0_6        (1 << 6)
#define K3_P0_5        (1 << 5)

#define K1_P2_31_DIR    FIO2DIR
#define K2_P0_6_DIR     FIO0DIR
#define K3_P0_5_DIR     FIO0DIR

#define K1_P2_31_PIN    FIO2PIN
#define K2_P0_6_PIN     FIO0PIN
#define K3_P0_5_PIN     FIO0PIN
# endif /* PG */

# if PS /* PS (procesor samplujacy) */
#define LED_P1_7      (1 <<  7)
#define LED_P1_6      (1 <<  6)
#define LED_P1_3      (1 <<  3)
#define LED_P1_17     (1 << 17)
#define LED_P1_16     (1 << 16)
#define LED_P1_15     (1 << 15)
#define LED_P1_14     (1 << 14)
#define LED_P1_2      (1 <<  2)
#define LED_P1_10     (1 << 10)
#define LED_P1_9      (1 <<  9)
#define LED_P1_8      (1 <<  8)
#define LED_P1_4      (1 <<  4)
#define LED_P1_1      (1 <<  1)
#define LED_P1_0      (1 <<  0)

#define LED_P1_7_ON     FIO1CLR
#define LED_P1_6_ON     FIO1CLR
#define LED_P1_3_ON     FIO1CLR
#define LED_P1_17_ON    FIO1CLR
#define LED_P1_16_ON    FIO1CLR
#define LED_P1_15_ON    FIO1CLR
#define LED_P1_14_ON    FIO1CLR
#define LED_P1_2_ON     FIO1CLR
#define LED_P1_10_ON    FIO1CLR
#define LED_P1_9_ON     FIO1CLR
#define LED_P1_8_ON     FIO1CLR
#define LED_P1_4_ON     FIO1CLR
#define LED_P1_1_ON     FIO1CLR
#define LED_P1_0_ON     FIO1CLR

#define LED_P1_7_OFF    FIO1SET
#define LED_P1_6_OFF    FIO1SET
#define LED_P1_3_OFF    FIO1SET
#define LED_P1_17_OFF   FIO1SET
#define LED_P1_16_OFF   FIO1SET
#define LED_P1_15_OFF   FIO1SET
#define LED_P1_14_OFF   FIO1SET
#define LED_P1_2_OFF    FIO1SET
#define LED_P1_10_OFF   FIO1SET
#define LED_P1_9_OFF    FIO1SET
#define LED_P1_8_OFF    FIO1SET
#define LED_P1_4_OFF    FIO1SET
#define LED_P1_1_OFF    FIO1SET
#define LED_P1_0_OFF    FIO1SET

#define LED_P1_7_PIN    FIO1PIN
#define LED_P1_6_PIN    FIO1PIN
#define LED_P1_3_PIN    FIO1PIN
#define LED_P1_17_PIN   FIO1PIN
#define LED_P1_16_PIN   FIO1PIN
#define LED_P1_15_PIN   FIO1PIN
#define LED_P1_14_PIN   FIO1PIN
#define LED_P1_2_PIN    FIO1PIN
#define LED_P1_10_PIN   FIO1PIN
#define LED_P1_9_PIN    FIO1PIN
#define LED_P1_8_PIN    FIO1PIN
#define LED_P1_4_PIN    FIO1PIN
#define LED_P1_1_PIN    FIO1PIN
#define LED_P1_0_PIN    FIO1PIN

#define LED_P1_7_DIR    FIO1DIR
#define LED_P1_6_DIR    FIO1DIR
#define LED_P1_3_DIR    FIO1DIR
#define LED_P1_17_DIR   FIO1DIR
#define LED_P1_16_DIR   FIO1DIR
#define LED_P1_15_DIR   FIO1DIR
#define LED_P1_14_DIR   FIO1DIR
#define LED_P1_2_DIR    FIO1DIR
#define LED_P1_10_DIR   FIO1DIR
#define LED_P1_9_DIR    FIO1DIR
#define LED_P1_8_DIR    FIO1DIR
#define LED_P1_4_DIR    FIO1DIR
#define LED_P1_1_DIR    FIO1DIR
#define LED_P1_0_DIR    FIO1DIR

#define K1_P0_31      (1 << 31)
#define K2_P0_27      (1 << 27)
#define K3_P1_30      (1 << 30)
#define K4_P1_31      (1 << 31)

#define K1_P0_31_DIR    FIO2DIR
#define K2_P0_27_DIR    FIO0DIR
#define K3_P1_30_DIR    FIO1DIR
#define K4_P1_31_DIR    FIO1DIR

#define K1_P0_31_PIN    FIO0PIN
#define K2_P0_27_PIN    FIO0PIN
#define K3_P1_30_PIN    FIO1PIN
#define K4_P1_31_PIN    FIO1PIN
# endif /* PS */

# if PM /* PM (procesor glowny) */
#define LED_P2_9       (1 << 9)
#define LED_P2_8       (1 << 8)
#define LED_P2_7       (1 << 7)
#define LED_P2_6       (1 << 6)
#define LED_P2_5       (1 << 5)
#define LED_P2_4       (1 << 4)
#define LED_P2_3       (1 << 3)
#define LED_P2_2       (1 << 2)
#define LED_P2_1       (1 << 1)
#define LED_P2_0       (1 << 0)
#define LED_P0_25     (1 << 25)
#define LED_P0_26     (1 << 26)
#define LED_P0_24     (1 << 24)
#define LED_P0_23     (1 << 23)

#define LED_P2_9_ON     FIO2CLR
#define LED_P2_8_ON     FIO2CLR
#define LED_P2_7_ON     FIO2CLR
#define LED_P2_6_ON     FIO2CLR
#define LED_P2_5_ON     FIO2CLR
#define LED_P2_4_ON     FIO2CLR
#define LED_P2_3_ON     FIO2CLR
#define LED_P2_2_ON     FIO2CLR
#define LED_P2_1_ON     FIO2CLR
#define LED_P2_0_ON     FIO2CLR
#define LED_P0_25_ON    FIO0CLR
#define LED_P0_26_ON    FIO0CLR
#define LED_P0_24_ON    FIO0CLR
#define LED_P0_23_ON    FIO0CLR

#define LED_P2_9_OFF    FIO2SET
#define LED_P2_8_OFF    FIO2SET
#define LED_P2_7_OFF    FIO2SET
#define LED_P2_6_OFF    FIO2SET
#define LED_P2_5_OFF    FIO2SET
#define LED_P2_4_OFF    FIO2SET
#define LED_P2_3_OFF    FIO2SET
#define LED_P2_2_OFF    FIO2SET
#define LED_P2_1_OFF    FIO2SET
#define LED_P2_0_OFF    FIO2SET
#define LED_P0_25_OFF   FIO0SET
#define LED_P0_26_OFF   FIO0SET
#define LED_P0_24_OFF   FIO0SET
#define LED_P0_23_OFF   FIO0SET

#define LED_P2_9_PIN    FIO2PIN
#define LED_P2_8_PIN    FIO2PIN
#define LED_P2_7_PIN    FIO2PIN
#define LED_P2_6_PIN    FIO2PIN
#define LED_P2_5_PIN    FIO2PIN
#define LED_P2_4_PIN    FIO2PIN
#define LED_P2_3_PIN    FIO2PIN
#define LED_P2_2_PIN    FIO2PIN
#define LED_P2_1_PIN    FIO2PIN
#define LED_P2_0_PIN    FIO2PIN
#define LED_P0_25_PIN   FIO0PIN
#define LED_P0_26_PIN   FIO0PIN
#define LED_P0_24_PIN   FIO0PIN
#define LED_P0_23_PIN   FIO0PIN

#define LED_P2_9_DIR    FIO2DIR
#define LED_P2_8_DIR    FIO2DIR
#define LED_P2_7_DIR    FIO2DIR
#define LED_P2_6_DIR    FIO2DIR
#define LED_P2_5_DIR    FIO2DIR
#define LED_P2_4_DIR    FIO2DIR
#define LED_P2_3_DIR    FIO2DIR
#define LED_P2_2_DIR    FIO2DIR
#define LED_P2_1_DIR    FIO2DIR
#define LED_P2_0_DIR    FIO2DIR
#define LED_P0_25_DIR   FIO0DIR
#define LED_P0_26_DIR   FIO0DIR
#define LED_P0_24_DIR   FIO0DIR
#define LED_P0_23_DIR   FIO0DIR

#define K1_P0_29      (1 << 29)
#define K2_P0_30      (1 << 30)

#define K1_P0_29_DIR    FIO0DIR
#define K2_P0_30_DIR    FIO0DIR

#define K1_P0_29_PIN    FIO0PIN
#define K2_P0_30_PIN    FIO0PIN
# endif /* PM */

# endif /* CANDC */
/*==============================================================================
 Globals
==============================================================================*/


/*==============================================================================
 Global function definitions
==============================================================================*/

/*------------------------------------------------------------------------------
 function name:   ledInit
 description:     leds initialization
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
void ledInit (void)
{
# if OLIMEX
   LED_DIR = LED0;
   LED_DIR |= LED1;
   LED0_OFF = LED0;  /* leds are turned off at start */
   LED1_OFF = LED1;
# endif

# if CANDC
# if PM
   //FIO2DIR |= LED_P2_9 | LED_P2_8 | LED_P2_7 | LED_P2_6 | LED_P2_5 | LED_P2_4 | LED_P2_3 | LED_P2_2 | LED_P2_1 | LED_P2_0;
   FIO0DIR |= LED_P0_25 | LED_P0_26 | LED_P0_24 | LED_P0_23;
   LED_P0_23_OFF |= LED_P0_23;
   LED_P0_24_OFF |= LED_P0_24;
   LED_P0_26_OFF |= LED_P0_26;
   LED_P0_25_OFF |= LED_P0_25;
# endif

# if PS
   FIO1DIR |= LED_P1_7 | LED_P1_6 | LED_P1_3 | LED_P1_17 | LED_P1_16 | LED_P1_15 | LED_P1_14 | LED_P1_2 | LED_P1_10 | LED_P1_9 | LED_P1_8 | LED_P1_4 | LED_P1_1 | LED_P1_0;
   LED_P1_7_OFF   |= LED_P1_7;
   LED_P1_6_OFF   |= LED_P1_6;
   LED_P1_3_OFF   |= LED_P1_3;
   LED_P1_17_OFF  |= LED_P1_17;
   LED_P1_16_OFF  |= LED_P1_16;
   LED_P1_15_OFF  |= LED_P1_15;
   LED_P1_14_OFF  |= LED_P1_14;
   LED_P1_2_OFF   |= LED_P1_2;
   LED_P1_10_OFF  |= LED_P1_10;
   LED_P1_9_OFF   |= LED_P1_9;
   LED_P1_8_OFF   |= LED_P1_8;
   LED_P1_4_OFF   |= LED_P1_4;
   LED_P1_1_OFF   |= LED_P1_1;
   LED_P1_0_OFF   |= LED_P1_0;
# endif

# if PG
   FIO0DIR |= LED_P0_12 |  LED_P0_13 | LED_P0_28 | LED_P0_27 | LED_P0_31;
   LED_P0_12_OFF |= LED_P0_12;
   LED_P0_13_OFF |= LED_P0_13;
   LED_P0_28_OFF |= LED_P0_28;
   LED_P0_27_OFF |= LED_P0_27;
   LED_P0_31_OFF |= LED_P0_31;
# endif
# endif /* CANDC */
}

/*------------------------------------------------------------------------------
 function name:   ledOn
 description:     turn on a led
 parameters:      led number
 returned value:  none
------------------------------------------------------------------------------*/
void ledOn  (uint8 num)
{
# if OLIMEX
   switch (num)
   {
   case 0:
      LED0_ON |= LED0;
      break;

   case 1:
      LED1_ON |= LED1;
      break;

   default:
      break; /* Olimex has only 2 leds */
   }
# endif


# if CANDC
# if PM
   switch (num)
   {
   case 0:
      LED_P0_23_ON |= LED_P0_23;
      break;

   case 1:
      LED_P0_24_ON |= LED_P0_24;
      break;

   case 2:
      LED_P0_26_ON |= LED_P0_26;
      break;

   case 3:
      LED_P0_25_ON |= LED_P0_25;
      break;

   default:
      break; /* Olimex has only 2 leds */
   }
# endif

# if PS
   switch (num)
   {
   case 0:
      LED_P1_7_ON |= LED_P1_7;
      break;

   case 1:
      LED_P1_1_ON |= LED_P1_1;
      break;

   case 2:
      LED_P1_0_ON |= LED_P1_0;
      break;

   default:
      break; /* Olimex has only 2 leds */
   }
# endif

# if PG
   switch (num)
   {
   case 0:
      LED_P0_12_ON |= LED_P0_12;
      break;

   case 1:
      LED_P0_13_ON |= LED_P0_13;
      break;

   case 2:
      LED_P0_28_ON |= LED_P0_28;
      break;

   case 3:
      LED_P0_27_ON |= LED_P0_27;
      break;

   case 4:
      LED_P0_31_ON |= LED_P0_31;
      break;

   default:
      break; /* Olimex has only 2 leds */
   }
# endif
# endif /* CANDC */
}

/*------------------------------------------------------------------------------
 function name:   ledOff
 description:     turn off a led
 parameters:      led number
 returned value:  none
------------------------------------------------------------------------------*/
void ledOff (uint8 num)
{
# if OLIMEX
   switch (num)
   {
   case 0:
      LED0_OFF |= LED0;
      break;

   case 1:
      LED1_OFF |= LED1;
      break;

   default:
      break; /* Olimex has only 2 leds */
   }
# endif


# if CANDC
# if PM
   switch (num)
   {
   case 0:
      LED_P0_23_OFF |= LED_P0_23;
      break;

   case 1:
      LED_P0_24_OFF |= LED_P0_24;
      break;

   case 2:
      LED_P0_26_OFF |= LED_P0_26;
      break;

   case 3:
      LED_P0_25_OFF |= LED_P0_25;
      break;

   default:
      break; /* Olimex has only 2 leds */
   }
# endif

# if PS
   switch (num)
   {
   case 0:
      LED_P1_7_OFF |= LED_P1_7;
      break;

   case 1:
      LED_P1_1_OFF |= LED_P1_1;
      break;

   case 2:
      LED_P1_0_OFF |= LED_P1_0;
      break;

   default:
      break; /* Olimex has only 2 leds */
   }
# endif

# if PG
   switch (num)
   {
   case 0:
      LED_P0_12_OFF |= LED_P0_12;
      break;

   case 1:
      LED_P0_13_OFF |= LED_P0_13;
      break;

   case 2:
      LED_P0_28_OFF |= LED_P0_28;
      break;

   case 3:
      LED_P0_27_OFF |= LED_P0_27;
      break;

   case 4:
      LED_P0_31_OFF |= LED_P0_31;
      break;

   default:
      break; /* Olimex has only 2 leds */
   }
# endif
# endif /* CANDC */
}

/*------------------------------------------------------------------------------
 function name:   ledBlink
 description:     change state of led
 parameters:      led number
 returned value:  none
------------------------------------------------------------------------------*/
void ledBlink  (uint8 num)
{
# if OLIMEX
   switch (num)
   {
   case 0:
      LED0_PIN ^= LED0;
      break;

   case 1:
      LED1_PIN ^= LED1;
      break;

   default:
      break; /* Olimex has only 2 leds */
   }
# endif


# if CANDC
# if PM
   switch (num)
   {
   case 0:
      LED_P0_23_PIN ^= LED_P0_23;
      break;

   case 1:
      LED_P0_24_PIN ^= LED_P0_24;
      break;

   case 2:
      LED_P0_26_PIN ^= LED_P0_26;
      break;

   case 3:
      LED_P0_25_PIN ^= LED_P0_25;
      break;

   default:
      break; /* Olimex has only 2 leds */
   }
# endif

# if PS
   switch (num)
   {
   case 0:
      LED_P1_7_PIN ^= LED_P1_7;
      break;

   case 1:
      LED_P1_1_PIN ^= LED_P1_1;
      break;

   case 2:
      LED_P1_0_PIN ^= LED_P1_0;
      break;

   default:
      break; /* Olimex has only 2 leds */
   }
# endif

# if PG
   switch (num)
   {
   case 0:
      LED_P0_12_PIN ^= LED_P0_12;
      break;

   case 1:
      LED_P0_13_PIN ^= LED_P0_13;
      break;

   case 2:
      LED_P0_28_PIN ^= LED_P0_28;
      break;

   case 3:
      LED_P0_27_PIN ^= LED_P0_27;
      break;

   case 4:
      LED_P0_31_PIN ^= LED_P0_31;
      break;

   default:
      break; /* Olimex has only 2 leds */
   }
# endif
# endif /* CANDC */
}


#if 0
/*------------------------------------------------------------------------------
 function name:   PG_ledsInit
 description:     leds initialization
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
void PG_ledsInit(void)
{
   FIO0DIR |= LED_P0_12 |  LED_P0_13 | LED_P0_28 | LED_P0_27 | LED_P0_31;
}
/*------------------------------------------------------------------------------
 function name:   PG_switchInit
 description:     switches initialization
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
void PG_switchInit(void)
{
   K1_P2_31_DIR &= ~K1_P2_31;
   K2_P0_6_DIR  &= ~K2_P0_6;
   K3_P0_5_DIR  &= ~K3_P0_5;
}
/*------------------------------------------------------------------------------
 function name:   PG_switchState
 description:     switches state checking
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
unsigned char PG_switchState(void)
{
   if ((K1_P2_31_PIN & K1_P2_31) == 0)
   {
      return 1;
   }
   if ((K2_P0_6_PIN & K2_P0_6) == 0)
   {
      return 2;
   }
   if ((K3_P0_5_PIN & K3_P0_5) == 0)
   {
      return 3;
   }
   return 0;
}

/*------------------------------------------------------------------------------
 function name:   PS_ledsInit
 description:     leds initialization
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
void PS_ledsInit(void)
{
   FIO1DIR |= LED_P1_7 | LED_P1_6 | LED_P1_3 | LED_P1_17 | LED_P1_16 | LED_P1_15 | LED_P1_14 | LED_P1_2 | LED_P1_10 | LED_P1_9 | LED_P1_8 | LED_P1_4 | LED_P1_1 | LED_P1_0;
}
/*------------------------------------------------------------------------------
 function name:   PS_switchInit
 description:     switches initialization
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
void PS_switchInit(void)
{
   K1_P0_31_DIR &= ~K1_P0_31;
   K2_P0_27_DIR &= ~K2_P0_27;
   K3_P1_30_DIR &= ~K3_P1_30;
   K4_P1_31_DIR &= ~K4_P1_31;
}
/*------------------------------------------------------------------------------
 function name:   PS_switchState
 description:     switches state checking
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
unsigned char PS_switchState(void)
{
   if ((K1_P0_31_PIN & K1_P0_31) == 0)
   {
      return 1;
   }
   if ((K2_P0_27_PIN & K2_P0_27) == 0)
   {
      return 2;
   }
   if ((K3_P1_30_PIN & K3_P1_30) == 0)
   {
      return 3;
   }
   if ((K4_P1_31_PIN & K4_P1_31) == 0)
   {
      return 4;
   }
   return 0;
}
/*------------------------------------------------------------------------------
 function name:   PM_ledsInit
 description:     leds initialization
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
void PM_ledsInit(void)
{
   FIO2DIR |= LED_P2_9 | LED_P2_8 | LED_P2_7 | LED_P2_6 | LED_P2_5 | LED_P2_4 | LED_P2_3 | LED_P2_2 | LED_P2_1 | LED_P2_0;
   FIO0DIR |= LED_P0_25 | LED_P0_26 | LED_P0_24 | LED_P0_23;
}
/*------------------------------------------------------------------------------
 function name:   PM_switchInit
 description:     switches initialization
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
void PM_switchInit(void)
{
   K1_P0_29_DIR &= ~K1_P0_29;
   K2_P0_30_DIR &= ~K2_P0_30;
}
/*------------------------------------------------------------------------------
 function name:   PM_switchState
 description:     switches state checking
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
unsigned char PM_switchState(void)
{
   if ((K1_P0_29_PIN & K1_P0_29) == 0)
   {
      return 1;
   }
   if ((K2_P0_30_PIN & K2_P0_30) == 0)
   {
      return 2;
   }
   return 0;
}
#endif
/******************************************************************************
* END OF FILE
******************************************************************************/



